import traceback

import aiohttp
from aiohttp import ClientTimeout

from common.log import logger

grab_wechat_code_url = "http://121.89.246.101:18001/userinfo?customerId={}"
find_case_url = "http://work.xi-ai.com/strategy/ai/findCaseInfoBySessionId?sessionId={}"
ql_add_wechat_url = "https://socket.chatlingxi.com:8081/wework/agree_add_wechat?session_id={}"
send_msg = "http://121.89.246.101:18005/sms?customerId={}"


class AddWechat(object):
    def __init__(self):
        pass

    async def get_case_info(self, session_id):
        '''
        对话接通 根据session_id获取用户信息
        Args:
            session_id:
        Returns:
        '''
        response_json = {}
        customerId = ""
        try:
            url = find_case_url.format(session_id)
            async with aiohttp.ClientSession(timeout=ClientTimeout(total=0.2)) as session:
                async with session.get(url) as response:
                    response_case = await response.json()
                    if response_case:
                        customerId = response_case.get("customerId", "")
                        logger.info(f"从电销平台查询的会话为:{session_id},查询结果是:{str(response_case)}")
                        return customerId
                    else:
                        logger.info(f"从电销平台查询的会话为:{session_id},未查询到结果")
        except Exception:
            response_json['message'] = "请求异常"
        return customerId

    async def grab_wechat_code(self, customer_id, sessionId):
        '''
        调用抓取加微串码服务
        Args:
            session_id:
        Returns:
        '''
        try:
            async with aiohttp.ClientSession(timeout=ClientTimeout(total=0.2)) as session:
                async with session.get(
                        grab_wechat_code_url.format(customer_id)) as response:
                    data = await response.text()
                    logger.info(f"调用RPA抓取企微串码:{customer_id}, sessionId: {sessionId} 抓取结果是:{str(data)}")
        except Exception:
            logger.info(f"调用RPA抓取企微串码:{customer_id}, sessionId: {sessionId} 抓取异常{traceback.format_exc()}")

    async def launch_add_wechat(self, session_id):
        '''
        发起加微
        Args:
            session_id:
        Returns:
        '''
        try:
            url = ql_add_wechat_url.format(session_id)
            async with aiohttp.ClientSession(timeout=ClientTimeout(total=2)) as session:
                async with session.get(url) as response:
                    response_json = await response.text()
                    logger.info(f"调用圈量发起加微:{session_id},发起结果是{str(response_json)}")
                    return response_json
        except Exception:
            logger.info(f"调用圈量发起加微:{session_id},发起异常{traceback.format_exc()}")
            return None

    async def launch_send_msg_wechat(self, sessionId, customer_id):
        '''
        发起加微
        Args:
            session_id:
        Returns:
        '''
        try:
            url = send_msg.format(customer_id)
            async with aiohttp.ClientSession(timeout=ClientTimeout(total=2)) as session:
                async with session.get(url) as response:
                    response_json = await response.text()
                    logger.info(f"发送加微短信:{sessionId} , 客户Id:{customer_id},发起结果是{str(response_json)}")
                    return response_json
        except Exception:
            logger.info(f"发送加微短信:{sessionId} ,客户Id:{customer_id},发起异常{traceback.format_exc()}")
            return None
